// pages/project/index.js
import  ajax from "../network/index"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addProjectUserStatue:false,
        addFileStatus:false,
        payClassify:['微信',"支付宝","银行卡","网银"],
        selectUser:'',
     data:{
        projectName:"",
        clientName:"",
        clientTel:"",
        contract:"",
        startWork:"",
        endTiem:"",
        payClassify:"",
        // proectPrice:"",
        projectAddress:"",
     },
     info:{
        projectName:"项目名称",
        clientName:"客户姓名",
        clientTel:"客户电话",
        contract:"签订时间",
        startWork:"开工时间",
        endTiem:"结束时间",
        payClassify:"付款方式",

        // proectPrice:"项目金额",
        projectAddress:"项目地址"
     }
        
    },

    addFile(e){
        console.log(e)
        this.setData({
            addFileStatus:!this.data.addFile
        })
    },
    projectNameClick(e){
        this.setData({
            'data.projectName':e.detail.value
        })
    },
    addProjectUser(){
        // if(this.data.addProjectUserStatue){

        //     wx.pageScrollTo({
        //       duration: 200,
        //      selector:"#addProject"
        //     })
        // }
        // this.setData({
        //     addProjectUserStatue:!this.data.addProjectUserStatue
        // })
        let data=this.data.selectUser
        wx.navigateTo({
          url: '/pages/selectUser/index?data='+JSON.stringify(data),
        })
    },
    clientNameClick(e){
        this.setData({
            'data.clientName':e.detail.value
        })
    },
    clientInputClick(e){
        this.setData({
            'data.clientTel':e.detail.value
        })
    },
    projectprice(e){
        this.setData({
            'data.proectPrice':e.detail.value
        })
    },
    projectAddress(e){
        this.setData({
            'data.projectAddress':e.detail.value
        })
    },
    selectContract(e){
        this.setData({
            'data.contract':e.detail.value
        })
    },
    selectStartTime(e){
        this.setData({
            'data.startWork':e.detail.value
        })
    },
    selectEndTime(e){
        this.setData({
            'data.endTiem':e.detail.value
        })
    },
    payClassifyClick(e){
        this.setData({
            'data.payClassify':this.data.payClassify[e.detail.value]
        })
    },
    //项目参与人员
    addUser(e){
        this.setData({
            'data.userIds':e.detail,
            addProjectUserStatue:false
        })
    },
    submit(){
        let arr = Object.keys(this.data.data)
       for(let v  of arr){
            if(this.data.data[v]==''){
                console.log(this.data.data[v],5555)
                wx.showToast({
                    icon:"none",
                    title: this.data.info[v]+'不能为空！',
                })
                return
            }
        }
        let userIds=[]
        this.data.selectUser.forEach(item=>{
            userIds.push(item.userId)
        })
        if(userIds.length==0){
            wx.showToast({
                icon:"none",
              title: '项目参与人员不能为空',
            })
            return
        }
        wx.showLoading({
          title: '正在创建项目....',
        })
        ajax({
            url:"/api/project/newAdd",
            method:"post",
            header:{
                'content-type':'application/json'
            },
            data:{
                signDate:this.data.data.contract,
                customerName:this.data.data.clientName,
                customerTel:this.data.data.clientTel,
                endDate:this.data.data.endTiem,
                // payAmount:this.data.data.proectPrice,
                payModel:this.data.data.payClassify,
                projectAddress:this.data.data.projectAddress,
                projectName:this.data.data.projectName,
                startDate:this.data.data.startWork,
                userIds:userIds
            }
        }).then(v=>{
            if(v.code!=200){
                wx.showToast({
                  title: v.msg,
                })
            }else{
                wx.hideLoading({
                  success: (res) => {},
                })
                wx.showToast({
                  title: '创建项目成功！',
                    success(){
                        wx.switchTab({
                            url: '/pages/menu/index',
                          })
                    }
                })
               
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})